package com.huirui.server.controller.admin;

import com.huirui.server.pojo.dto.CouponDto;
import com.huirui.server.pojo.result.PageResult;
import com.huirui.server.pojo.result.Result;
import com.huirui.server.service.CouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController("adminCouponController")
@RequestMapping("/admin/coupon")
@Slf4j
@Validated
@Tag(name = "优惠券管理", description = "小程序优惠券管理")
public class CouponController {

    @Resource
    private CouponService couponService;

    @PostMapping("/add")
    @Operation(summary = "新增优惠券")
    public Result<Void> addCoupon(@RequestBody @Valid CouponDto couponDto) {
        couponService.addNewCoupon(couponDto);
        return Result.success();
    }

    @GetMapping("/list")
    @Operation(summary = "查询优惠券")
    public Result<PageResult<CouponDto>> listCoupon(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam("page") Long page,
            @RequestParam("pageSize") Long pageSize
    ) {
        PageResult<CouponDto> res = couponService.listCoupon(keyword, status, page, pageSize);
        return Result.success(res);
    }

    @DeleteMapping("/delete/{couponId}")
    @Operation(summary = "删除优惠券")
    public Result<Void> deleteCoupon(@PathVariable("couponId") Long couponId) {
        couponService.deleteCoupon(couponId);
        return Result.success();
    }


    @PutMapping("/update")
    @Operation(summary = "更新优惠券")
    public Result<Void> updateCoupon(@RequestBody @Valid CouponDto couponDto) {
        couponService.updateCoupon(couponDto);
        return Result.success();
    }
}
